/*
 * p1520.cpp
 *
 *  Created on: 2013-7-23
 *      Author: zy
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 6010;
vector<int> son[maxn];
int a[maxn], f[maxn][2];
bool nr[maxn];//not root
void dfs(int u)
{
	f[u][1] = a[u];
	f[u][0] = 0;
	for (int i = 0; i < son[u].size(); i++)
	{
		int v = son[u][i];
		dfs(v);
		f[u][1] += f[v][0];
		f[u][0] += max(f[v][0], f[v][1]);
	}
}
int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			son[i].clear();
		}
		memset(nr, 0, sizeof(nr));
		int x, y;
		while (scanf("%d%d", &x, &y) && x && y)
		{
			son[y].push_back(x);
			nr[x] = 1;
		}
		int root = 0;
		for (int i = 1; i <= n; i++)
			if (!nr[i])
			{
				root = i;
				break;
			}
		dfs(root);
		printf("%d\n", max(f[root][0], f[root][1]));
	}
	return 0;
}
